package com.mes.biz.service.impl;

import com.mes.biz.domain.BizCustomerAccount;
import com.mes.biz.domain.BizCustomerPayable;
import com.mes.biz.domain.BizCustomerPayableDetail;
import com.mes.biz.domain.BizOrderProduct;
import com.mes.biz.domain.vo.ExportStatement;
import com.mes.biz.mapper.BizCustomerAccountMapper;
import com.mes.biz.mapper.BizCustomerPayableDetailMapper;
import com.mes.biz.service.BizCustomerAccountService;
import com.mes.biz.service.BizCustomerPayableDetailService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.exception.BizException;
import com.mes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * 对账单Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-04-30
 */
@Service
@Transactional(readOnly = true)
public class BizCustomerAccountServiceImpl extends BaseServiceImpl<BizCustomerAccountMapper, BizCustomerAccount> implements BizCustomerAccountService {

    @Resource
    private BizCustomerAccountMapper bizCustomerAccountMapper;
    @Resource
    private BizCustomerPayableDetailMapper bizCustomerPayableDetailMapper;
    @Autowired
    private BizCustomerAccountService bizCustomerAccountService;
    @Override
    public BizCustomerAccount getBizCustomerAccountInfo(String accountCode) {
        return bizCustomerAccountMapper.getBizCustomerAccountInfo(accountCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createCustomerAccount(BizCustomerPayable bizCustomerPayable) {
        BizCustomerAccount account = new BizCustomerAccount();
        account.setCustomerId(bizCustomerPayable.getCustomerId());
        BigDecimal amount = bizCustomerPayable.getPayableDetails().stream().map(BizCustomerPayableDetail::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        account.setTotalAmount(amount);
        account.setPayAmount(BigDecimal.ZERO);
        account.setActualAmount(BigDecimal.ZERO);
        account.setWipeAmount(BigDecimal.ZERO);
        account.setStatus(BaseEntity.STATUS_NORMAL);
        Random random = new Random();
        //把随机生成的数字转成字符串
        String str = String.valueOf(random.nextInt(9));
        for (int i = 0; i < 5; i++) {
            str += random.nextInt(9);
        }
        account.setAccountCode("DZD-"+str);
        String tenantId = SecurityUtils.getTenantId();
        account.setTenantId(tenantId);
        boolean res = save(account);
        for (int i = 0; i < bizCustomerPayable.getPayableDetails().size(); i++) {
            BizCustomerPayableDetail payableDetail = bizCustomerPayable.getPayableDetails().get(i);
            payableDetail.setAccountCode(account.getAccountCode());
            bizCustomerPayableDetailMapper.update(payableDetail);
        }
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomerAccount(BizCustomerPayable bizCustomerPayable) {
        List<BizCustomerPayableDetail> payableDetails = bizCustomerPayable.getPayableDetails();
        if (payableDetails !=null && payableDetails.size()>0){
            BizCustomerAccount bizCustomerAccountInfo = bizCustomerAccountService.getBizCustomerAccountInfo(bizCustomerPayable.getAccountCode());
            for (BizCustomerPayableDetail payableDetail : payableDetails) {
                payableDetail.setAccountCode(bizCustomerPayable.getAccountCode());
                bizCustomerPayableDetailMapper.update(payableDetail);
            }
            BigDecimal amount = payableDetails.stream().map(BizCustomerPayableDetail::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            bizCustomerAccountInfo.setTotalAmount(bizCustomerAccountInfo.getTotalAmount().add(amount));
            boolean res =  bizCustomerAccountService.save(bizCustomerAccountInfo);
            return true;
        }else {
            throw new BizException("明细不能为空");
        }
    }

    @Override
    public List<ExportStatement> exportStatement(BizCustomerAccount bizCustomerAccount) {
        return bizCustomerAccountMapper.exportStatement(bizCustomerAccount.getAccountCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAccountType(BizCustomerAccount bizCustomerAccount) {
        BizCustomerAccount bizCustomerAccount1 = get(bizCustomerAccount.getId());
        bizCustomerAccount1.setAccountType(bizCustomerAccount.getAccountType());
        return mapper.update(bizCustomerAccount1)>0;
    }
}
